﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ReactReduxTool.Utils;

namespace ReactReduxTool.UI
{

    public partial class FrmInit : Form
    {

        private bool mIsMouseDown;
        private Point mMousePoint;
        private string mTargetPath;
        private CmdReplyUtil cmdReplyUtil;

        public FrmInit()
        {
            InitializeComponent();
            cmdReplyUtil = CmdReplyUtil.GetInstance();
            cmdReplyUtil.StartCmd();
        }
        
        private void bgContainer_MouseDown(object sender, MouseEventArgs e)
        {
            mMousePoint = Cursor.Position;
            mIsMouseDown = true;
        }

        private void bgContainer_MouseMove(object sender, MouseEventArgs e)
        {
            if (mIsMouseDown && e.Button == MouseButtons.Left)
            {
                Point tempPos = Cursor.Position;
                Location = new Point(Location.X + (tempPos.X - mMousePoint.X), Location.Y + (tempPos.Y - mMousePoint.Y));
                mMousePoint = Cursor.Position;
            }
        }

        private void bgContainer_MouseUp(object sender, MouseEventArgs e)
        {
            mIsMouseDown = false;
            this.Focus();
        }
        
        private void btnChooseDirectory_Click(object sender, EventArgs e)
        {
            DialogUtil.ShowChooseTargetDirectory(folderName =>
            {
                txtTargetDirectory.Text = folderName;
                mTargetPath = folderName;
                new Thread(() =>
                {
                    cmdReplyUtil.RunCmdWithoutReply("cd " + folderName + " \r\n", () => { });
                }).Start();
            });
        }

        public string TargetPath
        {
            get { return mTargetPath; }
        }

        private void btnInitProject_Click(object sender, EventArgs e)
        {
            if (mTargetPath == null || mTargetPath.Trim().Equals(""))
            {
                MessageBox.Show("请选择您的工作目录!");
                return;
            }
            if (!Directory.Exists(mTargetPath))
            {
                MessageBox.Show("您选择的工作目录不存在!");
                return;
            }
            System.Diagnostics.Process.Start("explorer.exe", mTargetPath);
            this.DialogResult = DialogResult.Yes;
            //ExecNpmInitCmd();
        }

        private void imgClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }

        private void ExecNpmInitCmd()
        {
            string command = "npm init \r\n";
            new Thread(() =>
            {
                while (true)
                {
                    cmdReplyUtil.RunCmdWithReplay(command, (writer, msg) =>
                    {
                        DialogUtil.ShowInputDialog(msg, reply =>
                        {
                            if (reply == null || reply.Equals(""))
                            {
                                this.DialogResult = DialogResult.Yes;
                                return;
                            }
                            command = reply + "\r\n";
                        });
                    });
                }
            }).Start();
        }

        private void FrmInit_FormClosing(object sender, FormClosingEventArgs e)
        {
            cmdReplyUtil.CloseCmd();
        }
    }

}
